import { Injectable } from '@nestjs/common';

import { InjectRepository } from '@nestjs/typeorm';

import { DeleteResult, Repository, UpdateResult } from 'typeorm';

import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserEntity } from './entities/user.entity';

@Injectable()
export class UserService {
    constructor(@InjectRepository(UserEntity) private UserRepository: Repository<UserEntity>) {}

    findByUsername(username: string): Promise<UserEntity> {
        return this.UserRepository.findOneBy({ username });
    }

    async create(createUserDto: CreateUserDto): Promise<UserEntity> {
        console.log('service', createUserDto);
        const user = await this.UserRepository.save(createUserDto);
        console.log(user);
        return user;
    }

    async findAll(): Promise<UserEntity[]> {
        return this.UserRepository.find();
    }

    async findOne(id: number): Promise<UserEntity> {
        return this.UserRepository.findOneBy({ id });
    }

    update(id: number, updateUserDto: UpdateUserDto): Promise<UpdateResult> {
        return this.UserRepository.update(id, updateUserDto);
    }

    remove(id: number): Promise<DeleteResult> {
        return this.UserRepository.delete(id);
    }
}
